using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using CSP.SimpleCMS.Data;
using CSP.MembershipInterface.ObjectModel;

namespace CSP.SimpleCMS.DataModel
{
    /// <summary>Manages the operations related with the CaseRoleType</summary>
    public static class CaseRoleTypeDM
    {

        /// <summary>Returns a CaseRoleType with the ID sent by parameter</summary>
        /// <param name="caseRoleTypeID">The integer which identified the CaseRoleType</param>
        /// <returns>The CaseRoleType object with the ID</returns>
        public static CaseRoleType GetCaseRoleTypeById(int caseRoleTypeID)
        {
            CaseRoleType crmCaseRoleType = null;
            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPGetCaseRoleTypes, DMConstants.FieldCaseRoleTypeID, caseRoleTypeID);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count > 0)
            {
                crmCaseRoleType = ParseRow(dataSet.Tables[0].Rows[0]);
            }

            return crmCaseRoleType;
        }

        /// <summary>Returns a Dataset with all the CaseRoleTypes stored in the data source</summary>
        /// <returns>A Dataset with all the CaseRoleTypes</returns>
        public static DataSet GetCaseRoleTypes()
        {
            DataSet dataSet = new DataSet();

            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPGetCaseRoleTypes);
            }

            return dataSet;
        }

        /// <summary>Parses a datarow to a CaseRoleType</summary>
        /// <param name="row">The datarow to be parsed</param>
        /// <returns>A CaseRoleType object with the information stored in the datarow</returns>
        private static CaseRoleType ParseRow(DataRow row)
        {
            CaseRoleType crmCaseRoleType = new CaseRoleType();

            crmCaseRoleType.CaseRoleTypeId = Convert.ToInt32(row[DMConstants.FieldCaseRoleTypeID]);
            crmCaseRoleType.CaseRoleTypeTitle = row[DMConstants.FieldCaseRoleTypeTitle].ToString();

            return crmCaseRoleType;
        }

    }
}
